package com.kuyou.publiclibrary.utils;

import android.annotation.SuppressLint;
import android.app.KeyguardManager;
import android.content.Context;
import android.os.Build;
import android.os.PowerManager;

public class ScreenUtils {
    public static void unlockScreen(Context context) {
        KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
        KeyguardManager.KeyguardLock kl = km.newKeyguardLock("unLock");


        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        @SuppressLint("InvalidWakeLockTag") PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP
                | PowerManager.SCREEN_DIM_WAKE_LOCK, "bright");
        wl.acquire(1000);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            wl.release();
        }
    }

    /**
     * 判断是否锁屏状态
     *
     * @param context
     * @return
     */
    public final static boolean isScreenLocked(Context context) {
        if (null != context) {
            KeyguardManager mKeyguardManager = (KeyguardManager) context.getSystemService(
                    Context.KEYGUARD_SERVICE);
            //* 如果flag为true，表示有两种状态：a、屏幕是黑的 b、目前正处于解锁状态 。
            //* 如果flag为false，表示目前未锁屏
            //* 关闭屏幕不等于锁屏
            return mKeyguardManager.inKeyguardRestrictedInputMode();
        }

        return false;
    }

    /**
     * 为true，则表示屏幕“亮”了，否则屏幕“暗”了。
     *
     * @param context
     * @return
     */
    public final static boolean isScreenOn(Context context) {
        if (null != context) {
            PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            return pm.isScreenOn();
        }

        return false;
    }

    /**
     * 屏幕屏幕密度
     *
     * @param context
     * @return
     */
    public static float getScreenDensity(Context context) {
        if (null != context) {
            return context.getResources().getDisplayMetrics().density;
        }
        return 0;
    }

    /**
     * 屏幕宽度
     *
     * @param context
     * @return
     */
    public static int getScreenWidth(Context context) {
        if (null != context) {
            return context.getResources().getDisplayMetrics().widthPixels;
        }

        return 0;
    }

    /**
     * 屏幕高度
     */
    public static int getScreenHeight(Context context) {
        if (null != context) {
            return context.getResources().getDisplayMetrics().heightPixels;
        }

        return 0;
    }

    /**
     * 获取状态栏高度
     * @param context
     * @return
     */
    public static int getStatusBarHeight(Context context) {
        if (context == null) {
            return 0;
        }
        int statusHeight = -1;
        try {
//            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
//            Object object = clazz.newInstance();
//            int height = Integer.parseInt(clazz.getField("status_bar_height")
//                    .get(object).toString());
//            statusHeight = context.getResources().getDimensionPixelSize(height);
            //获取status_bar_height资源的ID
            int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                //根据资源ID获取响应的尺寸值
                statusHeight = context.getResources().getDimensionPixelSize(resourceId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statusHeight;
    }
}
